package com.shjy.client.delegate;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.RequestFuture;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.shjy.client.amap.LocationUtil;
import com.shjy.client.service.VersionUpdateService;

import org.json.JSONObject;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Created by ld on 2017/6/29.
 */

public class BaseWebviewDelegate {

    protected Context mContext;
    protected Handler mHandler;
    protected WebView mWebView;
    protected Intent mIntent;

    public BaseWebviewDelegate() {
    }

    public BaseWebviewDelegate(Context mContext) {
        this.mContext = mContext;
    }

    public BaseWebviewDelegate(Context mContext, Handler mHandler) {
        this.mContext = mContext;
        this.mHandler = mHandler;
    }

    public BaseWebviewDelegate(Context mContext, Handler mHandler, WebView webView, Intent mIntent) {
        this.mContext = mContext;
        this.mHandler = mHandler;
        this.mWebView = webView;
        this.mIntent = mIntent;
    }

    /**
     * setCookie
     * @param key
     * @param value
     */
    @JavascriptInterface
    public void setCookie(String key, String value) {
        SharedPreferences sharedPreferences = mContext.getSharedPreferences("cookie", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.commit();
    }

    /**
     * getCookie
     * @param key
     * @return value
     */
    @JavascriptInterface
    public String getCookie(String key) {
        SharedPreferences sharedPreferences = mContext.getSharedPreferences("cookie", Context.MODE_PRIVATE);
        return sharedPreferences.getString(key, "");
    }

    /**
     * 获取定位信息
     * @return value
     */
    @JavascriptInterface
    public String getLocation() {
        Map<String, String> resultMap = LocationUtil.getLocation(mContext);
        return resultMap.toString();
    }

    /**
     * 获取定位信息
     * @return value
     */
    @JavascriptInterface
    public String getCurrentLocation() {

        return getLocation();
    }

    @JavascriptInterface
    public void call(String tel) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_DIAL);
        intent.setData(Uri.parse("tel:" + tel));
        mContext.startActivity(intent);
    }

    @JavascriptInterface
    public String getVersion() {
        String versionName = "1.0";
        try {
            PackageInfo packageInfo = mContext.getApplicationContext().getPackageManager().getPackageInfo(mContext.getPackageName(), 0);
            versionName = packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionName;
    }

    @JavascriptInterface
    public String checkVersion() {
        String result = "0";
        String currentVersion = getVersion();

        String url = "http://139.224.80.49/shjy/fetchVersion.html?type=2";
        RequestFuture<String> future = RequestFuture.newFuture();
        StringRequest stringRequest = new StringRequest(url,future,future);
        RequestQueue requestQueue = Volley.newRequestQueue(mContext);
        requestQueue.add(stringRequest);
        try {
            String request = future.get();
            future.get(3000, TimeUnit.SECONDS);//添加请求超时

            JSONObject jsonObject = new JSONObject(request);
            String version = jsonObject.getString("version");
            String downloadUrl = jsonObject.getString("url");

            if (TextUtils.equals(version, currentVersion)) {
                result = "1";

                SharedPreferences sharedPreferences = mContext.getSharedPreferences("versonUpdate", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString("downloadUrl", downloadUrl);
                editor.commit();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    @JavascriptInterface
    public void updateApp() {

        mContext.startService(new Intent(mContext, VersionUpdateService.class));
    }
}
